<<<<<<< HEAD
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Forum extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		
	}
	
	/**
	 * 
	 * 共同的帖子内容
	 * 
	 */
	private function common_forum($tid){
		
		$this->load->model('Forum_model');	
		
		$data = array('page_size'=>8,'page_start'=>0,'fstatus'=>1,'folder'=>1,'is_shield'=>0);
		
		$vdata = array();
		//精彩推荐
		$wdata=$data;
		$wdata['first'] = 1;
		
		$wonderful_vdata = $this->Forum_model->invitation_list($wdata);
		$vdata['wonderful'] = $wonderful_vdata['forum_list'];
		
		//最新发帖
		$newest_vdata = $this->Forum_model->invitation_list($data);
		$vdata['newest'] = $newest_vdata['forum_list'];
		
		$vdata['theme'] = $this->Forum_model->get_theme_obj($tid);
		return $vdata;
		
	}
	
	/**
	 * 
	 * 论坛首页
	 */
	public function index(){
		
		$this->load->model('Forum_model');	
		
		$vdata['list'] = $this->Forum_model->forum_index();
		
		$vdata['title'] 		= '论坛 - '.CON_KEYWORDS;
  		$vdata['tab'] = 'forum';
		$this->load->view('forum/zhs_index',$vdata);
	}
	
	/**
	 * 
	 * 根据主题查找帖子列表
	 * @param unknown_type $id
	 */
	public function forumList($id){
		
		$vdata = $this->common_forum($id);
		
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		
		$this->load->model('Forum_model');	
		$data['where']['tid'] = $id;
		$data['where']['folder'] = 1;
		$data['fstatus'] = 1;
		$data['is_shield'] = 0;
		$data['page_size'] = 16;
		$data['page_start'] = (int)$per_page;
		$forum_vdata = $this->Forum_model->invitation_list($data);
		$vdata['forum_list'] = $forum_vdata['forum_list'];
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/forum/forumList/'.$id.'/?';
		$config['total_rows'] = $forum_vdata['forum_count'];
		$config['per_page'] = 16; 
		$this->pagination->initialize($config); 
		$vdata['title'] 		= $vdata['theme']['name'].'-论坛 - '.CON_KEYWORDS;
		
  		$vdata['tab'] = 'forum';
		$this->load->view('forum/zhs_list',$vdata);
	}
	
	
	/**
	 * 
	 * 帖子详情页
	 * @param unknown_type $id
	 */
	public function thread($id){
		
		$this->load->model('Forum_model');	
		
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		
		
		
		
		$forum_post = $this->Forum_model->get_forum_post_obj($id);
		$vdata = $this->Forum_model->set_hits_post($id);
		$vdata = $this->common_forum($forum_post['tid']);
		
		$cdata['where']['pid'] = $forum_post['id'];
		
		if(isset($_GET['is_floor']) && $this->input->get('is_floor')>0){
			$cdata['where']['is_floor'] = intval($this->input->get('is_floor'));
		}
		
		$cdata['page_size'] = 5;
		$cdata['page_start'] = (int)$per_page;
		
		$comment_list = $this->Forum_model->comment_list($cdata);
		$vdata['forum_list'] = $comment_list['forum_list'];
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/forum/thread/'.$id.'/?';
		$config['total_rows'] = $comment_list['forum_count'];
		$config['per_page'] = 5; 
		$this->pagination->initialize($config);
		
		$vdata['forum_post'] = $forum_post;
				
		$vdata['title'] 		= $forum_post['subject'].'-论坛 - '.CON_KEYWORDS;
		
		$vdata['tab'] = 'forum';
		$this->load->view('forum/zhs_thread',$vdata);
	}
	
	/**
	 * 
	 * 回复帖子
	 */
	public function forum_comment(){
		$user = $this->session->userdata('rsh_user'); //获取用户session信息
        if (isset($user['user_id']) && $user['user_id'] > 0) {
        	
        	$verify_code = $this->input->post('ve_code',true);
			if($verify_code == ''){
				echo '验证码不能为空';
				exit;
			}
			if ($verify_code!= $_SESSION['captcha']){
				echo '验证码不正确';
				exit;
			}

            $this->load->model('User_model');
            $this->load->model('Forum_model');
            
            $user_id = intval($user['user_id']);
            $userobj = $this->User_model->get_username_by_id($user_id);

            $content = trim($this->input->post("content",true));
            $pid = intval($this->input->post("id",true));
            
            $data = array('pid' => $pid,
                'comment' => htmlspecialchars($content),//防止用户XSS攻击
                'author' => $userobj['username'],
                'authorid' => $user_id,
                'add_time' => time()
            );
            
           $post_obj =  $this->Forum_model->get_forum_post_obj($pid);
            if($user_id == $post_obj['authorid']){
            	$data['is_floor'] = 1;
            }
            
           $res =  $this->Forum_model->add_comment($data);
           
            if ($res > 0) {
            	//修改帖子回复次数
            	$this->Forum_model->set_recovery_post($pid);
                echo 'ok';
                
            } else {
                echo 1;
            }
        } else {
            echo 2;
            exit;
        }
	}
	
	/**
	 * 
	 * 发帖
	 */
	public function forumPost($id){
		
		$this->load->model('Forum_model');
		
		
		$user = $this->session->userdata('rsh_user'); //获取用户session信息
		 if (isset($user['user_id']) && $user['user_id'] > 0) {
		 	$this->load->model('User_model');
			$user_id = intval($user['user_id']);
			
			//当前用户有多少草稿帖子
			$forum_list = $this->commonDraft($user_id); 	
		 	$vdata['post_count'] = $forum_list['forum_count'];
		 	
		 	
		 	if(isset($_POST['act']) && $_POST['act'] == 'submit'){
			 	
	            $userobj = $this->User_model->get_username_by_id($user_id);
	            
	            $type = intval($this->input->post("type",true));
	            
	             $data = array(
					'tid'=>intval($this->input->post('post_id',true)),
					'subject'=>trim($this->input->post('title',true)),
					'content'=>trim($this->input->post('content',true)),
					'folder'=>$type, 
				 	'add_time'=>time(),
				 	'author' => $userobj['username'],
		            'authorid' => $user_id,
				);
				
				$forum_id = $this->Forum_model->add_forum_post($data);
			 	if ($forum_id > 0) {
	               $ret['msg'] = $type;
	               $ret['cnt'] = $vdata['post_count']+1;
					echo json_encode($ret);
			   		exit;
	            } else {
	               $ret['msg'] = 'fail';
	                exit;
	            }
		 	}  
		 	  
		 	$vdata['post_id'] = $id;
		
			
			$vdata['title'] 		= '论坛  - 发表主题 -'.CON_KEYWORDS;
	  		$vdata['tab'] = 'forum';
			$this->load->view('forum/zhs_post',$vdata);
			
		 }else{
			 $this->js->go('/login.html');	
		}
		
	}
	
	private function commonDraft($user_id){
		$this->load->model('Forum_model');
		//当前用户有多少草稿帖子
		$cdata['where']['folder'] = 2;
		$cdata['where']['authorid'] = intval($user_id);
		$forum_list = $this->Forum_model->invitation_list($cdata);
	 	return $forum_list;
	 	
	 	
	}
	
	/**
	 * 
	 * 草稿箱
	 */
	function forumDraft(){
		$user = $this->session->userdata('rsh_user'); //获取用户session信息
		 if (isset($user['user_id']) && $user['user_id'] > 0) {
			$vdata = $this->commonDraft($user['user_id']); 	
		 	
		 }else{
		 	$this->js->go('/login.html');	
		 }
	}
	
	
	
	
=======
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Forum extends CI_Controller {
    
	function __construct(){
		parent::__construct();
		
	}
	
	/**
	 * 
	 * 共同的帖子内容
	 * 
	 */
	private function common_forum($tid){
		
		$this->load->model('Forum_model');	
		
		$data = array('page_size'=>8,'page_start'=>0,'fstatus'=>1,'folder'=>1,'is_shield'=>0);
		
		$vdata = array();
		//精彩推荐
		$wdata=$data;
		$wdata['first'] = 1;
		
		$wonderful_vdata = $this->Forum_model->invitation_list($wdata);
		$vdata['wonderful'] = $wonderful_vdata['forum_list'];
		
		//最新发帖
		$newest_vdata = $this->Forum_model->invitation_list($data);
		$vdata['newest'] = $newest_vdata['forum_list'];
		
		$vdata['theme'] = $this->Forum_model->get_theme_obj($tid);
		return $vdata;
		
	}
	
	/**
	 * 
	 * 论坛首页
	 */
	public function index(){
		
		$this->load->model('Forum_model');	
		
		$vdata['list'] = $this->Forum_model->forum_index();
		
		$vdata['title'] 		= '论坛 - '.CON_KEYWORDS;
  		$vdata['tab'] = 'forum';
		$this->load->view('forum/zhs_index',$vdata);
	}
	
	/**
	 * 
	 * 根据主题查找帖子列表
	 * @param unknown_type $id
	 */
	public function forumList($id){
		
		$vdata = $this->common_forum($id);
		
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		
		$this->load->model('Forum_model');	
		$data['where']['tid'] = $id;
		$data['where']['folder'] = 1;
		$data['fstatus'] = 1;
		$data['is_shield'] = 0;
		$data['page_size'] = 16;
		$data['page_start'] = (int)$per_page;
		$forum_vdata = $this->Forum_model->invitation_list($data);
		$vdata['forum_list'] = $forum_vdata['forum_list'];
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/forum/forumList/'.$id.'/?';
		$config['total_rows'] = $forum_vdata['forum_count'];
		$config['per_page'] = 16; 
		$this->pagination->initialize($config); 
		$vdata['title'] 		= $vdata['theme']['name'].'-论坛 - '.CON_KEYWORDS;
		
  		$vdata['tab'] = 'forum';
		$this->load->view('forum/zhs_list',$vdata);
	}
	
	
	/**
	 * 
	 * 帖子详情页
	 * @param unknown_type $id
	 */
	public function thread($id){
		
		$this->load->model('Forum_model');	
		
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		
		
		
		
		$forum_post = $this->Forum_model->get_forum_post_obj($id);
		$vdata = $this->Forum_model->set_hits_post($id);
		$vdata = $this->common_forum($forum_post['tid']);
		
		$cdata['where']['pid'] = $forum_post['id'];
		
		if(isset($_GET['is_floor']) && $this->input->get('is_floor')>0){
			$cdata['where']['is_floor'] = intval($this->input->get('is_floor'));
		}
		
		$cdata['page_size'] = 5;
		$cdata['page_start'] = (int)$per_page;
		
		$comment_list = $this->Forum_model->comment_list($cdata);
		$vdata['forum_list'] = $comment_list['forum_list'];
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/forum/thread/'.$id.'/?';
		$config['total_rows'] = $comment_list['forum_count'];
		$config['per_page'] = 5; 
		$this->pagination->initialize($config);
		
		$vdata['forum_post'] = $forum_post;
				
		$vdata['title'] 		= $forum_post['subject'].'-论坛 - '.CON_KEYWORDS;
		
		$vdata['tab'] = 'forum';
		$this->load->view('forum/zhs_thread',$vdata);
	}
	
	/**
	 * 
	 * 回复帖子
	 */
	public function forum_comment(){
		$user = $this->session->userdata('rsh_user'); //获取用户session信息
        if (isset($user['user_id']) && $user['user_id'] > 0) {
        	
        	$verify_code = $this->input->post('ve_code',true);
			if($verify_code == ''){
				echo '验证码不能为空';
				exit;
			}
			if ($verify_code!= $_SESSION['captcha']){
				echo '验证码不正确';
				exit;
			}

            $this->load->model('User_model');
            $this->load->model('Forum_model');
            
            $user_id = intval($user['user_id']);
            $userobj = $this->User_model->get_username_by_id($user_id);

            $content = trim($this->input->post("content",true));
            $pid = intval($this->input->post("id",true));
            
            $data = array('pid' => $pid,
                'comment' => htmlspecialchars($content),//防止用户XSS攻击
                'author' => $userobj['username'],
                'authorid' => $user_id,
                'add_time' => time()
            );
            
           $post_obj =  $this->Forum_model->get_forum_post_obj($pid);
            if($user_id == $post_obj['authorid']){
            	$data['is_floor'] = 1;
            }
            
           $res =  $this->Forum_model->add_comment($data);
           
            if ($res > 0) {
            	//修改帖子回复次数
            	$this->Forum_model->set_recovery_post($pid);
                echo 'ok';
                
            } else {
                echo 1;
            }
        } else {
            echo 2;
            exit;
        }
	}
	
	/**
	 * 
	 * 发帖
	 */
	public function forumPost($id){
		
		$this->load->model('Forum_model');
		
		
		$user = $this->session->userdata('rsh_user'); //获取用户session信息
		 if (isset($user['user_id']) && $user['user_id'] > 0) {
		 	$this->load->model('User_model');
			$user_id = intval($user['user_id']);
			
			//当前用户有多少草稿帖子
			$forum_list = $this->commonDraft($user_id); 	
		 	$vdata['post_count'] = $forum_list['forum_count'];
		 	
		 	
		 	if(isset($_POST['act']) && $_POST['act'] == 'submit'){
			 	
	            $userobj = $this->User_model->get_username_by_id($user_id);
	            
	            $type = intval($this->input->post("type",true));
	            
	             $data = array(
					'tid'=>intval($this->input->post('post_id',true)),
					'subject'=>trim($this->input->post('title',true)),
					'content'=>trim($this->input->post('content',true)),
					'folder'=>$type, 
				 	'add_time'=>time(),
				 	'author' => $userobj['username'],
		            'authorid' => $user_id,
				);
				
				$forum_id = $this->Forum_model->add_forum_post($data);
			 	if ($forum_id > 0) {
	               $ret['msg'] = $type;
	               $ret['cnt'] = $vdata['post_count']+1;
					echo json_encode($ret);
			   		exit;
	            } else {
	               $ret['msg'] = 'fail';
	                exit;
	            }
		 	}  
		 	  
		 	$vdata['post_id'] = $id;
		
			
			$vdata['title'] 		= '论坛  - 发表主题 -'.CON_KEYWORDS;
	  		$vdata['tab'] = 'forum';
			$this->load->view('forum/zhs_post',$vdata);
			
		 }else{
			 $this->js->go('/login.html');	
		}
		
	}
	
	private function commonDraft($user_id){
		$this->load->model('Forum_model');
		//当前用户有多少草稿帖子
		$cdata['where']['folder'] = 2;
		$cdata['where']['authorid'] = intval($user_id);
		$forum_list = $this->Forum_model->invitation_list($cdata);
	 	return $forum_list;
	 	
	 	
	}
	
	/**
	 * 
	 * 草稿箱
	 */
	function forumDraft(){
		$user = $this->session->userdata('rsh_user'); //获取用户session信息
		 if (isset($user['user_id']) && $user['user_id'] > 0) {
			$vdata = $this->commonDraft($user['user_id']); 	
		 	
		 }else{
		 	$this->js->go('/login.html');	
		 }
	}
	
	
	
	
>>>>>>> d47623b610d79a4eeb489500830c00d70933f414
}